package view.mediator.multidevs
{
	import com.google.maps.LatLng;
	import com.google.maps.Map;
	
	import controllers.commands.ClearMapCommand;
	import controllers.commands.QueryDevicesCommand;
	import controllers.managers.ScheduleManager;
	import controllers.managers.SingletonManager;
	import controllers.managers.UIInstanceManager;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import model.context.base.IContext;
	import model.registry.EnvConst;
	import model.vo.DeviceVO;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.logging.ILogger;
	
	import utils.DialogUtil;
	import utils.LogUtil;
	import utils.UIUtil;
	
	import view.ListPanel;
	import view.MapCanvas;
	import view.mediator.base.IMediator;
	import view.panel.DeviceSelectionPanel;
	import view.panel.MultiDeviceMonitor;
	
	/**
	 *  多设备监控
	 **/
	public class MultiDeviceMonitorMediator implements IMediator
	{
		public static var NAME : String = "MultiDeviceMonitorMediator.as";
		private var logger:ILogger =  LogUtil.getInst().getLogger( NAME );
		
		private var deviceMonitorPanel:MultiDeviceMonitor;
		private var listPanel:ListPanel = null;
		private var deviceSelectorPanel:DeviceSelectionPanel = null;
		public function MultiDeviceMonitorMediator()
		{
		}
		
		public function initEvents():void
		{}
		
		public function setView(view:UIComponent):void
		{
			updatePanels();
			deviceMonitorPanel = MultiDeviceMonitor(view);
			deviceMonitorPanel.btnReChoose.addEventListener(MouseEvent.CLICK, onReturnChoosePanelHandler);
			deviceMonitorPanel.btnMultiMonitor.addEventListener(MouseEvent.CLICK, onQueryHandler);
			deviceMonitorPanel.addEventListener("myClose", onClosePanelHandler);
			deviceMonitorPanel.dgMonitor.addEventListener(MouseEvent.DOUBLE_CLICK, onLocateHandler);
		}
		
		private function updatePanels():void
		{
			deviceSelectorPanel=DeviceSelectionPanel(UIInstanceManager.getUI(DeviceSelectionPanel.NAME));
			listPanel = ListPanel(UIInstanceManager.getUI(ListPanel.NAME));
		}
		
		/**
		 *  定位指定设备
		 * */
		private function onLocateHandler(evt:MouseEvent):void
		{
			var tgtVO:DeviceVO = DeviceVO(evt.currentTarget.selectedItem);
			var mapCanvas:MapCanvas = MapCanvas(UIInstanceManager.getUI(MapCanvas.NAME));
			if ( mapCanvas.map != null 
					&& tgtVO != null )
			{
				var latlng:LatLng = new LatLng( Number(tgtVO.latitude), Number(tgtVO.longitude) );
				mapCanvas.map.setCenter(latlng);
			}
		}
		
		/**
		 *  关闭窗体
		 **/
		private function onClosePanelHandler(evt:Event):void
		{
			onMainPage();
		}
		
		/**
		 *  查询
		 **/
		private function onQueryHandler(evt:MouseEvent):void
		{
			//拿出最后一次的查询命令
			var itemRaw : Object = SingletonManager.getServ(QueryDevicesCommand.NAME); 
			var cmd:QueryDevicesCommand = QueryDevicesCommand(itemRaw);
			if ( cmd != null )
			{
				cmd.execute();
			}
			else
			{
				DialogUtil.openError("手动刷新失败,请重试!", "错误");
			}
		}
		
		/**
		 * 重新选择
		 **/
		private function onReturnChoosePanelHandler(evt:MouseEvent):void
		{
			onMainPage();
		}
		
		private function onMainPage():void
		{
			updatePanels();
			listPanel = ListPanel(UIInstanceManager.getUI(ListPanel.NAME));
			listPanel.setHomeState();
			deviceMonitorPanel.visible = false;
			deviceSelectorPanel.visible = true;
			var map:Maper = Maper(FlexGlobals.topLevelApplication);
			UIUtil.setToTop(deviceSelectorPanel, map);
			
			new ClearMapCommand().execute();
			clearTimer();
		}
		
		/**
		 *  关闭定时器
		 * */
		private function clearTimer():void
		{
			logger.info("关闭定时器");
			var scheduleMgr:ScheduleManager =ScheduleManager( SingletonManager.getServ(ScheduleManager.NAME) );
			scheduleMgr.stopTask(QueryDevicesCommand.NAME);
		}
		
		public function setContext(context:IContext):void
		{
		}
		
	}
}